package com.qiangesoft.design.creational.abstractfactory;

/**
 * 抽象工厂模式
 */
public class AbstractFactory {
    public static void main(String[] args) {
        // 抽象工厂模式通过SoftwareFactory接口和其实现类来创建不同类型的操作系统和应用程序
        // 客户端代码可以根据需要选择不同的工厂实例来创建不同的产品组合

        // windows工厂
        SoftwareFactory windowsFactory = new WindowsFactory();
        // windows系统
        OperatingSystem windowsOS = windowsFactory.createOperatingSystem();
        // windows程序
        Application windowsApp = windowsFactory.createApplication();
        windowsOS.run();
        windowsApp.open();

        // linux工厂
        SoftwareFactory linuxFactory = new LinuxFactory();
        // linux系统
        OperatingSystem linuxOS = linuxFactory.createOperatingSystem();
        // linux程序
        Application linuxApp = linuxFactory.createApplication();
        linuxOS.run();
        linuxApp.open();
    }
}

/**
 * 1.产品接口1：操作系统
 */
interface OperatingSystem {
    void run();
}

/**
 * 2.具体产品1
 * Windows操作系统、Linux操作系统
 */
class WindowsOS implements OperatingSystem {
    @Override
    public void run() {
        System.out.println("Running Windows OS");
    }
}

class LinuxOS implements OperatingSystem {
    @Override
    public void run() {
        System.out.println("Running Linux OS");
    }
}

/**
 * 3.产品接口2：应用程序
 */
interface Application {
    void open();
}

/**
 * 4.具体产品2
 * WindowsJDK应用程序、LinuxJDK应用程序
 */
class WindowsJDKApplication implements Application {
    @Override
    public void open() {
        System.out.println("Opening WindowsJDK Application");
    }
}

class LinuxJDKApplication implements Application {
    @Override
    public void open() {
        System.out.println("Opening LinuxJDK Application");
    }
}

/**
 * 5.工厂接口
 */
interface SoftwareFactory {
    OperatingSystem createOperatingSystem();

    Application createApplication();
}

/**
 * 6.具体工厂
 * Windows工厂、Linux工厂
 */
class WindowsFactory implements SoftwareFactory {
    @Override
    public OperatingSystem createOperatingSystem() {
        return new WindowsOS();
    }

    @Override
    public Application createApplication() {
        return new WindowsJDKApplication();
    }
}

class LinuxFactory implements SoftwareFactory {
    @Override
    public OperatingSystem createOperatingSystem() {
        return new LinuxOS();
    }

    @Override
    public Application createApplication() {
        return new LinuxJDKApplication();
    }
}

